package com.luck.easyfoodbatch.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.entity.MineGrid;

import java.util.ArrayList;
import java.util.List;

/**
 * author：luck
 * project：LoveCare
 * package：com.tongyu.luck.lovecare.adapter
 * email：893855882@qq.com
 * data：16/8/12
 */
public class MineGridAdapter extends RecyclerView.Adapter<MineGridAdapter.MineGridViewHolder> {
    private Context mContext;
    private List<MineGrid> list = new ArrayList<>();
    protected OnItemClickListener mItemClickListener;

    public MineGridAdapter(Context mContext, List<MineGrid> list) {
        super();
        this.mContext = mContext;
        this.list = list;
    }

    @Override
    public MineGridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_grid, null);
        final MineGridViewHolder viewHolder = new MineGridViewHolder(view);
        //itemView 的点击事件
        if (mItemClickListener != null) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mItemClickListener.onItemClick(viewHolder.getAdapterPosition(), view);
                }
            });
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MineGridViewHolder holder, int position) {
        MineGrid mineGrid = list.get(position);
        holder.item_text.setText(mineGrid.getName());
        holder.item_img.setImageResource(mineGrid.getResources());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MineGridViewHolder extends RecyclerView.ViewHolder {
        ImageView item_img;
        TextView item_text;

        public MineGridViewHolder(View view) {
            super(view);
            item_img = (ImageView) view.findViewById(R.id.item_img);
            item_text = (TextView) view.findViewById(R.id.item_text);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }

}
